
from flask import Flask,render_template,request,redirect,url_for
from flask_sqlalchemy import SQLAlchemy
import os

from sqlalchemy import false


app=Flask(__name__)

#数据库管理部分
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///wiki.db'  # 数据库连接
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # 关闭警告
app.config['UPLOAD_FOLDER'] = 'static/uploads'  # 文件上传路径
db = SQLAlchemy(app)  # 初始化数据库实例

#region----数据库部分
# 稀有度关联表
role_xi_you_du = db.Table('role_xi_you_du',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('xi_you_du_id', db.Integer, db.ForeignKey('xi_you_du.id'))
)

# 标签关联表
role_tag = db.Table('role_tag',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'))
)

# 性格关联表
role_xing_ge = db.Table('role_xing_ge',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('xing_ge_id', db.Integer, db.ForeignKey('xing_ge.id'))
)

# 星球关联表
role_xing_qiu = db.Table('role_xing_qiu',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('xing_qiu_id', db.Integer, db.ForeignKey('xing_qiu.id'))
)

# 阵营关联表
role_zhen_ying = db.Table('role_zhen_ying',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('zhen_ying_id', db.Integer, db.ForeignKey('zhen_ying.id'))
)

# 突破材料关联表
role_tu_po_cai_liao = db.Table('role_tu_po_cai_liao',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('tu_po_cai_liao_id', db.Integer, db.ForeignKey('tu_po_cai_liao.id'))
)

# 技能关联表
role_ji_neng = db.Table('role_ji_neng',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('ji_neng_id', db.Integer, db.ForeignKey('ji_neng.id'))
)

# 武器关联表
role_weapon = db.Table('role_weapon',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('weapon_id', db.Integer, db.ForeignKey('weapon.id'))
)

# 属性关联表
role_shu_xing = db.Table('role_shu_xing',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('shu_xing_id', db.Integer, db.ForeignKey('shu_xing.id'))
)

# 刻印关联表
role_ke_yin = db.Table('role_ke_yin',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('ke_yin_id', db.Integer, db.ForeignKey('ke_yin.id'))
)

# 称号关联表
role_cheng_hao = db.Table('role_cheng_hao',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('cheng_hao_id', db.Integer, db.ForeignKey('cheng_hao.id'))
)

# 魂印关联表
role_hun_yin = db.Table('role_hun_yin',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('hun_yin_id', db.Integer, db.ForeignKey('hun_yin.id'))
)

# 配对推荐关联表
role_pei_dui_tui_jian = db.Table('role_pei_dui_tui_jian',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('pei_dui_tui_jian_id', db.Integer, db.ForeignKey('pei_dui_tui_jian.id'))
)

# 武器推荐关联表
role_wu_qi_tui_jian = db.Table('role_wu_qi_tui_jian',
	db.Column('role_id', db.Integer, db.ForeignKey('role.id')),
	db.Column('wu_qi_tui_jian_id', db.Integer, db.ForeignKey('wu_qi_tui_jian.id'))
)


class Role(db.Model):
	#角色自有属性
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100), nullable=False)
	xing_bie = db.Column(db.String(2), nullable=False)
	yuan_ming = db.Column(db.String(20), nullable=False)
	jian_jie = db.Column(db.String(200), nullable=False)
	shen_gao = db.Column(db.String(10), nullable=False)
	ti_zhong = db.Column(db.String(10), nullable=False)
	fa_se = db.Column(db.String(10), nullable=False)
	tong_se = db.Column(db.String(10), nullable=False)
	sheng_ri = db.Column(db.String(20), nullable=False)
	gu_shi = db.Column(db.String(200), nullable=False)
	fei_sheng = db.Column(db.String(600), nullable=False)

	#角色外部关联属性，无图，无简介
	xi_you_dus = db.relationship('Xi_you_du', secondary=role_xi_you_du, back_populates='roles')
	tags = db.relationship('Tag', secondary=role_tag, back_populates='roles')
	xing_ges = db.relationship('Xing_ge', secondary=role_xing_ge, back_populates='roles')
	xing_qius = db.relationship('Xing_qiu', secondary=role_xing_qiu, back_populates='roles')
	zhen_yings = db.relationship('Zhen_ying', secondary=role_zhen_ying, back_populates='roles')
	tu_po_cai_liaos = db.relationship('Tu_po_cai_liao', secondary=role_tu_po_cai_liao, back_populates='roles')
	
	ji_nengs = db.relationship('Ji_neng', secondary=role_ji_neng, back_populates='roles')
	
	weapons = db.relationship('Weapon', secondary=role_weapon, back_populates='roles')
	shu_xings = db.relationship('Shu_xing', secondary=role_shu_xing, back_populates='roles')
	ke_yins = db.relationship('Ke_yin', secondary=role_ke_yin, back_populates='roles')
	cheng_haos = db.relationship('Cheng_hao', secondary=role_cheng_hao, back_populates='roles')
	hun_yins = db.relationship('Hun_yin', secondary=role_hun_yin, back_populates='roles')
	pei_dui_tui_jians = db.relationship('Pei_dui_tui_jian', secondary=role_pei_dui_tui_jian, back_populates='roles')
	wu_qi_tui_jians = db.relationship('Wu_qi_tui_jian', secondary=role_wu_qi_tui_jian, back_populates='roles')

class Xi_you_du(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_xi_you_du, back_populates='xi_you_dus')

class Tag(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_tag, back_populates='tags')

class Xing_ge(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_xing_ge, back_populates='xing_ges')

class Xing_qiu(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_xing_qiu, back_populates='xing_qius')

class Zhen_ying(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_zhen_ying, back_populates='zhen_yings')

class Tu_po_cai_liao(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_tu_po_cai_liao, back_populates='tu_po_cai_liaos')

class Ji_neng(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_ji_neng, back_populates='ji_nengs')

class Weapon(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_weapon, back_populates='weapons')

class Shu_xing(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_shu_xing, back_populates='shu_xings')

class Ke_yin(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_ke_yin, back_populates='ke_yins')

class Cheng_hao(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_cheng_hao, back_populates='cheng_haos')

class Hun_yin(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_hun_yin, back_populates='hun_yins')

class Pei_dui_tui_jian(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_pei_dui_tui_jian, back_populates='pei_dui_tui_jians')

class Wu_qi_tui_jian(db.Model):
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(20), nullable=False)
	photo_path = db.Column(db.String(100))
	jian_jie = db.Column(db.String(200))
	roles = db.relationship('Role', secondary=role_wu_qi_tui_jian, back_populates='wu_qi_tui_jians')

with app.app_context():
	db.create_all()
#数据库管理部分
#endregion


#region----角色管理部分
#角色列表显示
@app.route('/role_list')
def role_list():
	roles = Role.query.all()
	return render_template('role_list.html',roles=roles)

@app.route('/role_list/<int:role_id>')
def show_role(role_id):
	role = Role.query.get(role_id)
	return render_template('show_role.html',role=role)


#角色增删改改查
@app.route('/add_role', methods=['GET','POST'])
def add_role():
#get方法
	if request.method == 'GET':
		# 获取所有模型的全部记录
		xi_you_dus = Xi_you_du.query.all()
		tags = Tag.query.all()
		xing_ges = Xing_ge.query.all()
		xing_qius = Xing_qiu.query.all()
		zhen_yings = Zhen_ying.query.all()
		tu_po_cai_liaos = Tu_po_cai_liao.query.all()
		ji_nengs = Ji_neng.query.all()
		weapons = Weapon.query.all()
		shu_xings = Shu_xing.query.all()
		ke_yins = Ke_yin.query.all()
		cheng_haos = Cheng_hao.query.all()
		hun_yins = Hun_yin.query.all()
		pei_dui_tui_jians = Pei_dui_tui_jian.query.all()
		wu_qi_tui_jians = Wu_qi_tui_jian.query.all()
		# 在render_template中传递所有参数
		return render_template('add_role.html',
			xi_you_dus=xi_you_dus,
			tags=tags,
			xing_ges=xing_ges,
			xing_qius=xing_qius,
			zhen_yings=zhen_yings,
			tu_po_cai_liaos=tu_po_cai_liaos,
			ji_nengs=ji_nengs,
			weapons=weapons,
			shu_xings=shu_xings,
			ke_yins=ke_yins,
			cheng_haos=cheng_haos,
			hun_yins=hun_yins,
			pei_dui_tui_jians=pei_dui_tui_jians,
			wu_qi_tui_jians=wu_qi_tui_jians)

#post方法	
	if request.method == 'POST':
		name = request.form.get('name')
		xing_bie = request.form.get('xing_bie')
		yuan_ming = request.form.get('yuan_ming')
		jian_jie = request.form.get('jian_jie')
		shen_gao = request.form.get('shen_gao')
		ti_zhong = request.form.get('ti_zhong')
		fa_se = request.form.get('fa_se')
		tong_se = request.form.get('tong_se')
		sheng_ri = request.form.get('sheng_ri')
		gu_shi = request.form.get('gu_shi')
		fei_sheng = request.form.get('fei_sheng')
		new_role = Role(
			name=name,
			photo_path='',
			xing_bie=xing_bie,
			yuan_ming=yuan_ming,
			jian_jie=jian_jie,
			shen_gao=shen_gao,
			ti_zhong=ti_zhong,
			fa_se=fa_se,
			tong_se=tong_se,
			sheng_ri=sheng_ri,
			gu_shi=gu_shi,
			fei_sheng=fei_sheng)
		file = request.files['file']

		#获取所有多对多关系的ID列表并赋值给new_role
		xi_you_du_ids = request.form.getlist('xi_you_dus')
		if xi_you_du_ids:
			for xi_you_du_id in xi_you_du_ids:
				new_role.xi_you_dus.append(Xi_you_du.query.get(xi_you_du_id))
		
		tag_ids = request.form.getlist('tags')
		if tag_ids:
			for tag_id in tag_ids:
				new_role.tags.append(Tag.query.get(tag_id))
		
		xing_ge_ids = request.form.getlist('xing_ges')
		if xing_ge_ids:
			for xing_ge_id in xing_ge_ids:
				new_role.xing_ges.append(Xing_ge.query.get(xing_ge_id))
		
		xing_qiu_ids = request.form.getlist('xing_qius')
		if xing_qiu_ids:
			for xing_qiu_id in xing_qiu_ids:
				new_role.xing_qius.append(Xing_qiu.query.get(xing_qiu_id))
		
		zhen_ying_ids = request.form.getlist('zhen_yings')
		if zhen_ying_ids:
			for zhen_ying_id in zhen_ying_ids:
				new_role.zhen_yings.append(Zhen_ying.query.get(zhen_ying_id))
		
		tu_po_cai_liao_ids = request.form.getlist('tu_po_cai_liaos')
		if tu_po_cai_liao_ids:
			for tu_po_cai_liao_id in tu_po_cai_liao_ids:
				new_role.tu_po_cai_liaos.append(Tu_po_cai_liao.query.get(tu_po_cai_liao_id))
		
		ji_neng_ids = request.form.getlist('ji_nengs')
		if ji_neng_ids:
			for ji_neng_id in ji_neng_ids:
				new_role.ji_nengs.append(Ji_neng.query.get(ji_neng_id))
		
		weapon_ids = request.form.getlist('weapons')
		if weapon_ids:
			for weapon_id in weapon_ids:
				new_role.weapons.append(Weapon.query.get(weapon_id))
		
		shu_xing_ids = request.form.getlist('shu_xings')
		if shu_xing_ids:
			for shu_xing_id in shu_xing_ids:
				new_role.shu_xings.append(Shu_xing.query.get(shu_xing_id))
		
		ke_yin_ids = request.form.getlist('ke_yins')
		if ke_yin_ids:
			for ke_yin_id in ke_yin_ids:
				new_role.ke_yins.append(Ke_yin.query.get(ke_yin_id))
		
		cheng_hao_ids = request.form.getlist('cheng_haos')
		if cheng_hao_ids:
			for cheng_hao_id in cheng_hao_ids:
				new_role.cheng_haos.append(Cheng_hao.query.get(cheng_hao_id))
		
		hun_yin_ids = request.form.getlist('hun_yins')
		if hun_yin_ids:
			for hun_yin_id in hun_yin_ids:
				new_role.hun_yins.append(Hun_yin.query.get(hun_yin_id))
		
		pei_dui_tui_jian_ids = request.form.getlist('pei_dui_tui_jians')
		if pei_dui_tui_jian_ids:
			for pei_dui_tui_jian_id in pei_dui_tui_jian_ids:
				new_role.pei_dui_tui_jians.append(Pei_dui_tui_jian.query.get(pei_dui_tui_jian_id))
		
		wu_qi_tui_jian_ids = request.form.getlist('wu_qi_tui_jians')
		if wu_qi_tui_jian_ids:
			for wu_qi_tui_jian_id in wu_qi_tui_jian_ids:
				new_role.wu_qi_tui_jians.append(Wu_qi_tui_jian.query.get(wu_qi_tui_jian_id))
		
		# 最后提交到数据库
		db.session.add(new_role)
		db.session.commit()

		file_extension = file.filename.rsplit('.', 1)[1].lower()
		file.filename = f"Role_{new_role.id}.{file_extension}"
		file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
		new_role.photo_path = f"uploads/{file.filename}"
		
		db.session.commit()

	return redirect(url_for('role_list'))

@app.route('/update_role/<int:role_id>', methods=['GET','POST'])
def update_role(role_id):
	role=Role.query.get(role_id)
#get方法	
	if request.method == 'GET':
		xi_you_dus = Xi_you_du.query.all()
		tags = Tag.query.all()
		xing_ges = Xing_ge.query.all()
		xing_qius = Xing_qiu.query.all()
		zhen_yings = Zhen_ying.query.all()
		tu_po_cai_liaos = Tu_po_cai_liao.query.all()
		ji_nengs = Ji_neng.query.all()
		weapons = Weapon.query.all()
		shu_xings = Shu_xing.query.all()
		ke_yins = Ke_yin.query.all()
		cheng_haos = Cheng_hao.query.all()
		hun_yins = Hun_yin.query.all()
		pei_dui_tui_jians = Pei_dui_tui_jian.query.all()
		wu_qi_tui_jians = Wu_qi_tui_jian.query.all()
		# 在render_template中传递所有参数
		return render_template('update_role.html',
			role=role,
			xi_you_dus=xi_you_dus,
			tags=tags,
			xing_ges=xing_ges,
			xing_qius=xing_qius,
			zhen_yings=zhen_yings,
			tu_po_cai_liaos=tu_po_cai_liaos,
			ji_nengs=ji_nengs,
			weapons=weapons,
			shu_xings=shu_xings,
			ke_yins=ke_yins,
			cheng_haos=cheng_haos,
			hun_yins=hun_yins,
			pei_dui_tui_jians=pei_dui_tui_jians,
			wu_qi_tui_jians=wu_qi_tui_jians)
#post方法	
	if request.method == 'POST':
		role.name = request.form.get('name')
		role.xing_bie = request.form.get('xing_bie')
		role.yuan_ming = request.form.get('yuan_ming')
		role.jian_jie = request.form.get('jian_jie')
		role.shen_gao = request.form.get('shen_gao')
		role.ti_zhong = request.form.get('ti_zhong')
		role.fa_se = request.form.get('fa_se')
		role.tong_se = request.form.get('tong_se')
		role.sheng_ri = request.form.get('sheng_ri')
		role.gu_shi = request.form.get('gu_shi')
		role.fei_sheng = request.form.get('fei_sheng')

		file = request.files.get('file')
		if file != None and file.filename != '':
			file_extension = file.filename.rsplit('.', 1)[1].lower()
			file.filename = f"Role_{role.id}.{file_extension}"
			file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
		
		# 获取并更新稀有度关系
		xi_you_du_ids = request.form.getlist('xi_you_dus')
		role.xi_you_dus = []
		if xi_you_du_ids:
			for xi_you_du_id in xi_you_du_ids:
				role.xi_you_dus.append(Xi_you_du.query.get(xi_you_du_id))

		# 获取并更新标签关系
		tag_ids = request.form.getlist('tags')
		role.tags = []
		if tag_ids:
			for tag_id in tag_ids:
				role.tags.append(Tag.query.get(tag_id))

		# 获取并更新性格关系
		xing_ge_ids = request.form.getlist('xing_ges')
		role.xing_ges = []
		if xing_ge_ids:
			for xing_ge_id in xing_ge_ids:
				role.xing_ges.append(Xing_ge.query.get(xing_ge_id))

		# 获取并更新星球关系
		xing_qiu_ids = request.form.getlist('xing_qius')
		role.xing_qius = []
		if xing_qiu_ids:
			for xing_qiu_id in xing_qiu_ids:
				role.xing_qius.append(Xing_qiu.query.get(xing_qiu_id))

		# 获取并更新阵营关系
		zhen_ying_ids = request.form.getlist('zhen_yings')
		role.zhen_yings = []
		if zhen_ying_ids:
			for zhen_ying_id in zhen_ying_ids:
				role.zhen_yings.append(Zhen_ying.query.get(zhen_ying_id))

		# 获取并更新突破材料关系
		tu_po_cai_liao_ids = request.form.getlist('tu_po_cai_liaos')
		role.tu_po_cai_liaos = []
		if tu_po_cai_liao_ids:
			for tu_po_cai_liao_id in tu_po_cai_liao_ids:
				role.tu_po_cai_liaos.append(Tu_po_cai_liao.query.get(tu_po_cai_liao_id))

		# 获取并更新技能关系
		ji_neng_ids = request.form.getlist('ji_nengs')
		role.ji_nengs = []
		if ji_neng_ids:
			for ji_neng_id in ji_neng_ids:
				role.ji_nengs.append(Ji_neng.query.get(ji_neng_id))

		# 获取并更新武器关系
		weapon_ids = request.form.getlist('weapons')
		role.weapons = []
		if weapon_ids:
			for weapon_id in weapon_ids:
				role.weapons.append(Weapon.query.get(weapon_id))

		# 获取并更新属性关系
		shu_xing_ids = request.form.getlist('shu_xings')
		role.shu_xings = []
		if shu_xing_ids:
			for shu_xing_id in shu_xing_ids:
				role.shu_xings.append(Shu_xing.query.get(shu_xing_id))

		# 获取并更新刻印关系
		ke_yin_ids = request.form.getlist('ke_yins')
		role.ke_yins = []
		if ke_yin_ids:
			for ke_yin_id in ke_yin_ids:
				role.ke_yins.append(Ke_yin.query.get(ke_yin_id))

		# 获取并更新称号关系
		cheng_hao_ids = request.form.getlist('cheng_haos')
		role.cheng_haos = []
		if cheng_hao_ids:
			for cheng_hao_id in cheng_hao_ids:
				role.cheng_haos.append(Cheng_hao.query.get(cheng_hao_id))

		# 获取并更新婚姻关系
		hun_yin_ids = request.form.getlist('hun_yins')
		role.hun_yins = []
		if hun_yin_ids:
			for hun_yin_id in hun_yin_ids:
				role.hun_yins.append(Hun_yin.query.get(hun_yin_id))

		# 获取并更新配对推荐关系
		pei_dui_tui_jian_ids = request.form.getlist('pei_dui_tui_jians')
		role.pei_dui_tui_jians = []
		if pei_dui_tui_jian_ids:
			for pei_dui_tui_jian_id in pei_dui_tui_jian_ids:
				role.pei_dui_tui_jians.append(Pei_dui_tui_jian.query.get(pei_dui_tui_jian_id))

		# 获取并更新武器推荐关系
		wu_qi_tui_jian_ids = request.form.getlist('wu_qi_tui_jians')
		role.wu_qi_tui_jians = []
		if wu_qi_tui_jian_ids:
			for wu_qi_tui_jian_id in wu_qi_tui_jian_ids:
				role.wu_qi_tui_jians.append(Wu_qi_tui_jian.query.get(wu_qi_tui_jian_id))

		# 提交到数据库
		db.session.commit()

	return redirect(url_for('role_list'))

@app.route('/delete_role/<int:role_id>', methods=['POST'])
def delete_role(role_id):

	role_to_delete = Role.query.get(role_id)

	# 解除所有多对多关联关系
	for xi_you_du in role_to_delete.xi_you_dus:
		xi_you_du.roles.remove(role_to_delete)

	for tag in role_to_delete.tags:
		tag.roles.remove(role_to_delete)

	for xing_ge in role_to_delete.xing_ges:
		xing_ge.roles.remove(role_to_delete)

	for xing_qiu in role_to_delete.xing_qius:
		xing_qiu.roles.remove(role_to_delete)

	for zhen_ying in role_to_delete.zhen_yings:
		zhen_ying.roles.remove(role_to_delete)

	for tu_po_cai_liao in role_to_delete.tu_po_cai_liaos:
		tu_po_cai_liao.roles.remove(role_to_delete)

	for ji_neng in role_to_delete.ji_nengs:
		ji_neng.roles.remove(role_to_delete)

	for weapon in role_to_delete.weapons:
		weapon.roles.remove(role_to_delete)

	for shu_xing in role_to_delete.shu_xings:
		shu_xing.roles.remove(role_to_delete)

	for ke_yin in role_to_delete.ke_yins:
		ke_yin.roles.remove(role_to_delete)

	for cheng_hao in role_to_delete.cheng_haos:
		cheng_hao.roles.remove(role_to_delete)

	for hun_yin in role_to_delete.hun_yins:
		hun_yin.roles.remove(role_to_delete)

	for pei_dui_tui_jian in role_to_delete.pei_dui_tui_jians:
		pei_dui_tui_jian.roles.remove(role_to_delete)

	for wu_qi_tui_jian in role_to_delete.wu_qi_tui_jians:
		wu_qi_tui_jian.roles.remove(role_to_delete)

	# 提交关系解除操作
	db.session.commit()

	file_path = os.path.join('static', role_to_delete.photo_path)
	os.remove(file_path)

	# 最后删除角色记录
	db.session.delete(role_to_delete)
	db.session.commit()
	return render_template('role_list.html')



#通用模型
@app.route('/smodel_list/<smodel_type_name>', methods=['GET','POST'])
def smodel_list(smodel_type_name):
	smodel_type = globals()[smodel_type_name]
	smodels = smodel_type.query.all()
	return render_template('smodel_list.html',smodel_type_name=smodel_type_name,smodels=smodels)


@app.route('/add_smodel/<smodel_type_name>', methods=['GET','POST'])
def add_smodel(smodel_type_name):
	if request.method == 'GET':
		return render_template('add_smodel.html', smodel_type_name=smodel_type_name)
	if request.method == 'POST':
		name = request.form.get('name')
		jian_jie = request.form.get('jian_jie')
		file = request.files['file']
		smodel_type = globals()[smodel_type_name]
		new_smodel = smodel_type(name=name, jian_jie=jian_jie, photo_path='')

		db.session.add(new_smodel)
		db.session.commit()

		file_extension = file.filename.rsplit('.', 1)[1].lower()
		file.filename = f"{smodel_type.__name__}{new_smodel.id}.{file_extension}"
		file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
		new_smodel.photo_path = f"uploads/{file.filename}"
		db.session.commit()

	return redirect(url_for('smodel_list' , smodel_type_name=smodel_type_name))

@app.route('/update_smodel/<smodel_type_name>/<int:id>', methods=['GET','POST'])
def update_smodel(smodel_type_name, id):
	smodel_type = globals()[smodel_type_name]
	smodel = smodel_type.query.get(id)
	if request.method == 'GET':
		return render_template('update_smodel.html', smodel=smodel)
	if request.method == 'POST':
		smodel.name = request.form.get('name')
		smodel.jian_jie = request.form.get('jian_jie')

		file = request.files.get('file')
		if file != None and file.filename != '':
			file_extension = file.filename.rsplit('.', 1)[1].lower()
			file.filename = f"{smodel_type_name}{smodel.id}.{file_extension}"
			file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
	return redirect(url_for('smodel_list' , smodel_type_name=smodel_type_name))

@app.route('/delete_smodel/<smodel_type_name>/<int:id>', methods=['POST'])
def delete_smodel(smodel_type_name, id):
	smodel_type = globals()[smodel_type_name]
	smodel_to_delete = smodel_type.query.get(id)
	lower_smodel_type_name =  str.lower(smodel_type_name)
	# 解除所有多对多关联关系
	for role in smodel_to_delete.roles:
		getattr(role, f"{lower_smodel_type_name}s").remove(smodel_to_delete)
	# 提交关系解除操作
	db.session.commit()

	file_path = file_path = os.path.join('static', smodel_to_delete.photo_path)
	os.remove(file_path)

	# 最后删除角色记录
	db.session.delete(smodel_to_delete)
	db.session.commit()
	return redirect(url_for('smodel_list' , smodel_type_name=smodel_type_name))

@app.route('/show_smodel/<smodel_type_name>/<int:id>')
def show_smodel(smodel_type_name, id):
	smodel_type = globals()[smodel_type_name]
	smodel = smodel_type.query.get(id)
	return render_template('show_smodel.html', smodel_type_name=smodel_type_name, smodel=smodel)

@app.route('/')
def index():
	return render_template("index.html")



@app.route('/weapon_list')
def weapon_list():
	weapons = Weapon.query.all()
	return render_template("weapon_list.html",weapons=weapons)


if __name__=="__main__":
	app.run(debug=True)